package com.work.saturn.main;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.immo.libcommon.base.BaseActivity;
import com.immo.libcommon.utils.SharePreferenceUtils;
import com.work.saturn.R;
import com.work.saturn.account.LoginActivity;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.PermissionNo;
import com.yanzhenjie.permission.PermissionYes;
import com.yanzhenjie.permission.Rationale;
import com.yanzhenjie.permission.RationaleListener;

import java.util.List;

/**
 * @author Administrator
 * @content 启动页
 * @date 2018/7/10
 */
public class StartActivty extends BaseActivity {
    private static final int REQUEST_CODE_PERMISSION_MULTI = 101;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start);
        getPerMission();

    }

    /**
     * 获取权限
     */
    public void getPerMission() {
        AndPermission.with(this)
                .requestCode(REQUEST_CODE_PERMISSION_MULTI).permission(
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE
        ).callback(this).rationale(new RationaleListener() {
            @Override
            public void showRequestPermissionRationale(int requestCode, Rationale rationale) {
                AndPermission.rationaleDialog(StartActivty.this, rationale).show();
            }
        }).start();
    }

    @PermissionYes(REQUEST_CODE_PERMISSION_MULTI)
    private void getMultiYes(@NonNull List<String> grantedPermissions) {
        isLogin();
    }
    @PermissionNo(REQUEST_CODE_PERMISSION_MULTI)
    private void getMultiNo(@NonNull List<String> deniedPermissions) {
        finish();
    }
    /**
     * 是否登录
     */
    public void isLogin() {
         String token= (String) SharePreferenceUtils.get(StartActivty.this,"token","");
         Intent intent;
        if (token != null && token.length() > 0) {
            intent = new Intent(StartActivty.this, MainActivity.class);
        } else {
            intent = new Intent(StartActivty.this, LoginActivity.class);
        }
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
        finish();
    }


}
